//
//  TSCVCReg2GatherInfoGuide.m
//  Tsc_IOS
//
//  Created by Rio on 15-5-29.
//  Copyright (c) 2015年 idream. All rights reserved.
//

#import "TSCVCReg2GatherInfoGuide.h"
#import "TSCSDK.h"
#import "TSCVCReg2SearchShop.h"
#import "TSCVCReg2Welcome.h"

@interface TSCVCReg2GatherInfoGuide () {
    // 输入项
    NSInteger _iUserRole;
    NSInteger _iCityId;
    
    // 其他vc返回项
    NSInteger _rShopId;
}

@property (weak, nonatomic) IBOutlet UITextField *tfSelectShop;

@property (weak, nonatomic) IBOutlet UITextField *tfTel;

@property (weak, nonatomic) IBOutlet UIButton *btnEnter;

@end

@implementation TSCVCReg2GatherInfoGuide

#pragma mark 获取并校验输入
- (void) getVCInput:(NSNotificationCenter *)notification {
    [super getVCInput:notification];
    
    _iUserRole = [[_inputDic objectForKey:AEKI_USER_ROLE] intValue];
    _iCityId = [[_inputDic objectForKey:AEKI_CITY_ID] intValue];
    
    NSAssert(_iUserRole != 0, @"%s: 输入参数必须包括：%@！", __FUNCTION__, AEKI_USER_ROLE);
    NSAssert(_iCityId != 0, @"%s: 输入参数必须包括：%@！", __FUNCTION__, AEKI_CITY_ID);
}

+ (id)vcReg2GatherInfoGuideWithStoryboard {
    UIStoryboard * sb = [UIStoryboard storyboardWithName:UI_STORYBOARD_NAME_MAIN bundle:[NSBundle mainBundle]];
    return [sb instantiateViewControllerWithIdentifier:UI_VC_NAME_REG2_GATHER_INFO_GUIDE];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 注册事件——监听代理
    _tfSelectShop.delegate = self;
    _tfTel.delegate = self;
    
    // 设置自定义导航栏
    _nbc = [[TSCMyNaviBarCreator alloc]initAndCreateWithViewController:self hasGoBackButton:YES goBackVC:nil hasAppLogo:YES titleResourceKey:@"title_last_step" iconResourceNameArray:nil iconClickedListenerArray:nil];
    
    // ===== 设置UI
    // 设置店铺名称右侧icon
    [TSCMyUIUtil addDownTriangleIconToView:_tfSelectShop direction:IconDirectionRight];
}

- (void) viewWillAppear:(BOOL)animated {
    // 如果从搜索店铺vc返回，则输入中应该有shopId、shopName
    _rShopId = [[_inputDic objectForKey:AEKI_SHOP_ID] integerValue];
    NSString * selectedShopName = [_inputDic objectForKey:AEKI_SHOP_NAME];
    
    if (selectedShopName) {
        _tfSelectShop.text = selectedShopName;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

/* private */ - (void) resignAllFirstResponder {
    [_tfSelectShop resignFirstResponder];
    [_tfTel resignFirstResponder];
}

// return键收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

// 点击空白区域收回键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self resignAllFirstResponder];
}

#pragma mark IBAction 注册事件

// 注册事件——选择店铺
- (IBAction)tfSelectShopTouchDown:(id)sender {
    TSCVCReg2SearchShop * vcReg2SearchShop = [TSCVCReg2SearchShop vcReg2SearchShopWithStoryBoard];
    [[self navigationController] pushViewController:vcReg2SearchShop animated:YES];
    
    // VC间传递参数
    NSDictionary * inputDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithLong:_iCityId] forKey:AEKI_CITY_ID];
    [[NSNotificationCenter defaultCenter]addObserver:vcReg2SearchShop selector:@selector(SELECTOR_GET_VC_INPUT) name:UI_VC_NAME_REG2_SEARCH_SHOP object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:UI_VC_NAME_REG2_SEARCH_SHOP object:inputDic];
}

// 注册事件——开始”商场快车“之旅！
- (IBAction)btnEnterAppClicked:(id)sender {
    // 收起”可能“打开的键盘
    [self resignAllFirstResponder];
    
    // 进入注册逻辑
    [TSCVCReg2Welcome register2_preWithVC:self ud:_ud userRole:_iUserRole cityId:_iCityId shopId:_rShopId tel:[TSCMyUtil trim:_tfTel.text]];
}


@end
